package com.bmslz.monitor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bmslz.monitor.base.annotation.AutoLog;
import com.bmslz.monitor.base.dto.AlarmModelSaveDto;
import com.bmslz.monitor.base.dto.AlarmModelSearchDto;
import com.bmslz.monitor.base.entity.AlarmModel;
import com.bmslz.monitor.base.entity.AlarmModelRule;
import com.bmslz.monitor.service.IAlarmModelService;
import com.bmslz.monitor.base.vo.AlarmModelVo;
import com.bmslz.monitor.base.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 告警模板
 * @Author: bobo
 * @Date: 2025-08-10
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "告警模板")
@RestController
@RequestMapping("/sys/alarmModel")
public class AlarmModelController {
    @Autowired
    private IAlarmModelService alarmModelService;

    /**
     * 分页列表查询
     *
     * @param searchDto
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "告警模板-分页列表查询")
    @ApiOperation(value = "告警模板-分页列表查询", notes = "告警模板-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AlarmModelSearchDto searchDto,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<AlarmModel> page = new Page<AlarmModel>(pageNo, pageSize);
        IPage<AlarmModel> pageList = alarmModelService.page(page, searchDto.wrapper());
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "告警模板-添加")
    @ApiOperation(value = "告警模板-添加", notes = "告警模板-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AlarmModelSaveDto dto) {
        alarmModelService.add(dto);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "告警模板-编辑")
    @ApiOperation(value = "告警模板-编辑", notes = "告警模板-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<?> edit(@RequestBody AlarmModelSaveDto dto) {
        alarmModelService.myUpdateById(dto);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "告警模板-通过id删除")
    @ApiOperation(value = "告警模板-通过id删除", notes = "告警模板-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        alarmModelService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "告警模板-批量删除")
    @ApiOperation(value = "告警模板-批量删除", notes = "告警模板-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.alarmModelService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "告警模板-通过id查询")
    @ApiOperation(value = "告警模板-通过id查询", notes = "告警模板-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AlarmModelVo vo = alarmModelService.customGetById(id);
        return Result.OK(vo);
    }

    /**
     * 根据项目id和key查找告警规则
     *
     * @param attKey
     * @param projectId
     * @return
     */
    @AutoLog(value = "监控项目元数据-根据项目id和key查找告警规则")
    @ApiOperation(value = "监控项目元数据-根据项目id和key查找告警规则", notes = "监控项目元数据-根据项目id和key查找告警规则")
    @GetMapping(value = "/queryRuleByKey")
    public Result<?> queryAttKeyList(@RequestParam("projectId") String projectId, @RequestParam("attKey") String attKey) {
        List<AlarmModelRule> res = alarmModelService.queryRuleByKey(projectId, attKey);
        return Result.OK(res);
    }
}
